home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / ip_frag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  4.9 KB  |  167 lines

  1. /*
  2.   ip_frag.c
  3.  
  4.   Dug Song <dugsong@anzen.com>
  5.  
  6.   Copyright (c) 1999 Anzen Computing. All rights reserved.
  7.  
  8.   Redistribution and use in source and binary forms, with or without
  9.   modification, are permitted provided that the following conditions
  10.   are met:
  11.   
  12.   1. Redistributions of source code must retain the above copyright
  13.      notice, this list of conditions and the following disclaimer.
  14.   2. Redistributions in binary form must reproduce the above copyright
  15.      notice, this list of conditions and the following disclaimer in the
  16.      documentation and/or other materials provided with the distribution.
  17.   3. All advertising materials mentioning features or use of this software
  18.      must display the following acknowledgement:
  19.      This product includes software developed by Anzen Computing.
  20.   4. Neither the name of Anzen Computing nor the names of its
  21.      contributors may be used to endorse or promote products derived
  22.      from this software without specific prior written permission.
  23.  
  24.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25.   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26.   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  27.   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  28.   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  30.   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31.   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  32.   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  33.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  34.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.  
  36.   $Id: ip_frag.c,v 1.18 1999/06/25 19:11:37 dugsong Exp $
  37. */
  38.  
  39. #include "config.h"
  40. #ifdef HAVE_SYS_PARAM_H
  41. #include <sys/param.h>
  42. #endif
  43. #include <libnet.h>
  44. #include "list.h"
  45. #include "ip_frag.h"
  46.  
  47. #if defined(OpenBSD) && (OpenBSD < 199905)
  48. /* XXX - OpenBSD 2.4 sendto() bug - see fragrouter/test/mies.c. */
  49. #define MINLASTFRAG    4
  50. #else
  51. #define MINLASTFRAG    0
  52. #endif
  53.  
  54. int ip_frag_preserve_headers = 0;
  55.  
  56. void
  57. ip_frag_init(int preserve_headers)
  58. {
  59.   ip_frag_preserve_headers = preserve_headers;
  60. }
  61.  
  62. ELEM *
  63. ip_frag_make(u_char *pkt, int pktlen, int fragsize)
  64. {
  65.   ELEM *new, *list = NULL;
  66.   struct ip *iph = (struct ip *)pkt;
  67.   int ip_hl = iph->ip_hl * 4;
  68.   u_short ip_len = ntohs(iph->ip_len);
  69.   u_char *ip_blob = pkt + ip_hl;
  70.   u_char *ip_end = pkt + ip_len;
  71.   u_char *p, *data;
  72.   int len = fragsize;
  73.   
  74.   /* Preserve transport protocol header, if specified. */
  75.   if (ip_frag_preserve_headers) {
  76.     switch (iph->ip_p) {
  77.     case IPPROTO_TCP:
  78.       len = ((struct tcphdr *)(pkt + ip_hl))->th_off * 4;
  79.       break;
  80.     case IPPROTO_UDP:
  81.       len = UDP_H;
  82.       break;
  83.     case IPPROTO_ICMP:
  84.       len = ICMP_ECHO_H; /* XXX */
  85.       break;
  86.     case IPPROTO_IGMP:
  87.       len = IGMP_H;
  88.       break;
  89.     default:
  90.       len = fragsize; /* XXX */
  91.       break;
  92.     }
  93.     if (len & 7) len = (len & ~7) + 8;
  94.   }
  95.   /* Make sure fragmentation is valid for this packet. */
  96.   if (pktlen <= ip_hl + len + MINLASTFRAG || fragsize % 8 != 0)
  97.     return NULL;
  98.  
  99.   /* Fragment packet. */
  100.   for (p = ip_blob ; p < ip_end ; ) {
  101.  
  102.     /* Copy in IP header and data. */
  103.     if (!(data = malloc(ip_hl + len))) return NULL;
  104.     memcpy(data, pkt, ip_hl);
  105.     memcpy(data + ip_hl, p, len);
  106.  
  107.     /* Correct IP length, IP fragment offset. */
  108.     ((struct ip *)data)->ip_len = htons(ip_hl + len);
  109.     ((struct ip *)data)->ip_off =
  110.       htons(((p + len < ip_end) ? IP_MF : 0) | ((p - ip_blob) >> 3));
  111.  
  112.     /* Add to our IP fragment list. */
  113.     new = list_elem(data, ip_hl + len);
  114.     free(data);
  115.     if (!(list = list_add(list, new)))
  116.       return NULL;
  117.  
  118.     /* Determine next fragment size. */
  119.     p += len;
  120.     len = ip_end - p;
  121.     if (len > fragsize + MINLASTFRAG)
  122.       len = fragsize;
  123.   }
  124.   return (list->head);
  125. }
  126.  
  127. ELEM *
  128. ip_frag_add_overwrite(ELEM *list)
  129. {
  130.   ELEM *f, *new;
  131.   u_char nulls[PACKET], *newdata;
  132.   int newoff, newlen;
  133.  
  134.   memset(nulls, 0, sizeof(nulls));
  135.   
  136.   for (f = list ; f && f->next ; f = f->next) {
  137.     struct ip *iph = (struct ip *)f->data;
  138.     int ip_hl = iph->ip_hl * 4;
  139.  
  140.     /* Build new null data fragment, half the length of the current fragment
  141.        and overlapping its latter half. */
  142.     newlen = (ntohs(iph->ip_len) - ip_hl) / 2;
  143.     newoff = ntohs(iph->ip_off) + (newlen >> 3);
  144.  
  145.     /* Copy in IP header and data. */
  146.     if (!(newdata = malloc(ip_hl + newlen))) return NULL;
  147.     memcpy(newdata, f->data, ip_hl);
  148.     memcpy(newdata + ip_hl, nulls, newlen);
  149.  
  150.     /* Correct IP length, IP fragment offset. */
  151.     ((struct ip *)newdata)->ip_len = htons(ip_hl + newlen);
  152.     ((struct ip *)newdata)->ip_off = htons(newoff);
  153.  
  154.     /* Add null fragment after current fragment. */
  155.     new = list_elem(newdata, IP_H + newlen);
  156.     free(newdata);
  157.     
  158.     if (!list_add(f, new))
  159.       return NULL;
  160.     
  161.     /* Swap them. */
  162.     if (!list_swap(f))
  163.       return NULL;
  164.   }
  165.   return list;
  166. }
  167.